let laypage = layui.laypage;
let form = layui.form; 

let data = {
    pagenum	:2,
    pagesize :2,
}

// 页面显示
function listshow() {
    $.ajax({
        url:'/my/article/list',
        data:data,
        success:function(res){
            console.log(res)
            if(res.status===0){
                let  html = template('list-show',res)

                $('tbody').html(html)
                showpage(res.total)
            }
        }
    })
}
listshow()
// 分页功能
    function showpage(t){
            laypage.render({
                elem: 'test1', 
                count: t,
                limit: data.pagesize, 
                limits: [2, 3, 5, 10], 
                curr: data.pagenum, 
                layout: [ 'prev', 'page', 'next', 'count', 'limit', 'skip'],
                jump: function (obj, first) {
                    if(!first){
                        data.pagenum = obj.curr;
                        data.pagesize = obj.limit;
                        listshow()
                    }
                    
                }
            });
        
    }



// 筛选功能
    $.ajax({
        url:'/my/article/cates',
        success:function(res){
            let html = template('choose',res)
            $('.cos').html(html)
            form.render('select');

        }
    })

    $('.search').on('submit',function(ev){
        ev.preventDefault()
        let val= $(this).serializeArray()
        console.log(val)
                data.cate_id = val[0].value;
                data.state = val[1].value;
                data.pagenum=1
                listshow()
    })




// 删除功能

$('tbody').on('click','button:contains("删除")',function(){
    let id = $(this).data('id')
    layer.confirm('确认删除么', {icon: 3, title:'提示'}, function(index){
        //do something
        $.ajax({
            url:'/my/article/delete/'+id,
            success:function(res){
               if(res.status===0){
                layer.msg(res.message); 
                listshow()
               }
            }
        })
        layer.close(index);
      });
})


// 时间格式

template.defaults.imports.dateFormat = function (str) {
    let date = new Date(str);
    // 获取年月日时分秒
    let y = date.getFullYear();
    let m = addZero(date.getMonth() + 1);
    let d = addZero(date.getDate());
    let h = addZero(date.getHours());
    let i = addZero(date.getMinutes());
    let s = addZero(date.getSeconds());
    return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
}

function addZero (n) {
    return n < 10 ? '0' + n : n ;
}